/*
 * ﻿Copyright (C) 2009-2013 NewMain Softech
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package com.newmainsoftech.gwetons.server.logging;

import com.google.gwt.core.client.GWT;
import com.google.gwt.inject.rebind.adapter.GinModuleAdapter;
import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
//import com.newmainsoftech.gwetons.client.logging.LoggerWrapGinjector;
import com.newmainsoftech.gwetons.client.logging.JsniLoggerWrapGinjector;
import com.newmainsoftech.gwetons.client.logging.LoggerWrapGinModule;
import com.newmainsoftech.gwetons.client.logging.LoggerWrapGinjector;
import com.newmainsoftech.gwetons.shared.logging.LoggerWrap;
import com.newmainsoftech.gwetons.shared.logging.LoggerWrapInjector;

public class LoggerWrapInjectorImpl implements LoggerWrapInjector {
	public static class LoggerWrapGinModuleAdapter implements Module {
		public void configure( Binder binder) {
			binder.install( new GinModuleAdapter( new LoggerWrapGinModule()));
		}
	}
	private static LoggerWrapGinModuleAdapter loggerWrapGinModuleAdapter = new LoggerWrapGinModuleAdapter();
	
	private static Injector injector;
		public synchronized static Injector getInjector() {
			if ( LoggerWrapInjectorImpl.injector == null) {
				if ( !GWT.isClient()) {
					LoggerWrapInjectorImpl.injector = Guice.createInjector( loggerWrapGinModuleAdapter);
				}
				else {
					throw new UnsupportedOperationException(
							"By default, it is not supported to use this for GWT client code. " 
							.concat( "Either set your custom Injector instance to this before actual ") 
							.concat( "injection happens or use %1$s or %2$s .")
							.replace( "%1$s", JsniLoggerWrapGinjector.class.getSimpleName())
							.replace( "%2$s", LoggerWrapGinjector.class.getSimpleName())
							);
				}
			}
			return LoggerWrapInjectorImpl.injector;
		}
		public synchronized static void setInjector( Injector injector) {
			LoggerWrapInjectorImpl.injector = injector;
		}
		
	public LoggerWrap getLoggerWrap() {
		return LoggerWrapInjectorImpl.getInjector().getInstance( LoggerWrap.class);
	}
}
